<template>
<div class="after-sale-reason views-container">
  <el-dialog @close="saveDialogForm" :visible.sync="dialogTableVisible" custom-class="reset-pwd wlm-form-dialog dialog-table">
    <div class="wlm-form" v-if="dialogTableVisible">
      <div class="wlm-form-header wlm-dialog-fixheader">
        <span class="wlm-dialog-inlenr">添加商圈</span>
      </div>
      <el-scrollbar wrap-class="scrollbar-wrapper">
        <div class="wlm-form-content">
          <div class="wlm-dialog-main">
            <div class="wlm-dialog-content">
              <el-form
:ref="formFormatData.key"
:model="formFormatData.formData"
               :rules="formFormatData.rules"
size="small"
label-width="120px"
class="retail-form"
label-position="right">
                <el-form-item label="商圈名字">
                  <el-input v-model.trim="formFormatData.formData.circle_name" :disabled="formFormatData.formData.default==1?true:false">
                  </el-input>
                </el-form-item>
                <el-form-item label="排序">
                  <el-input v-model.trim="formFormatData.formData.sort" :disabled="formFormatData.formData.default==1?true:false">
                  </el-input>
                    <div class="form-help">注:数值越大越靠前</div>
                </el-form-item>
                <el-form-item label="是否展示">
                  <el-radio-group v-model.trim="formFormatData.formData.is_display">
                    <el-radio :label="1">开启</el-radio>
                    <el-radio :label="2">关闭</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
      </el-scrollbar>
    </div>
    <footer class="flex-row flex-align-c flex-justify-c wlm-dialog-fixfooter">
      <el-button size="small" @click="formFormatSubmit" type="primary">保存</el-button>
    </footer>
  </el-dialog>
  <div class="wlm-table">
    <div class="wlm-table-hearder-btn">
      <el-button size="small" type="primary" @click="showdialog(1)">添加商圈</el-button>
    </div>
    <div class="wlm-table-content">
      <el-table  :ref="tableFormatData.Circle.key" :data="tableFormatData.Circle.tableData" @selection-change="handleSelectionChange" style="width: 100%">
        <!-- <el-table-column type="selection" width="55">
        </el-table-column> -->
        <el-table-column prop="date" label="id" >
          <template slot-scope="scope">
            <span> {{scope.row.circle_id}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="名称" >
          <template slot-scope="scope">
            <span> {{scope.row.circle_name}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="排序" >
          <template slot-scope="scope">
            <span> {{scope.row.sort}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="创建时间" >
          <template slot-scope="scope">
            <span>{{scope.row.creation_time| parseTime('{y}-{m}-{d} {h}:{i}:{s}')}}</span>
          </template>
        </el-table-column>
         <el-table-column prop="name" label="状态" >
              <template slot-scope="scope">

            <el-switch :active-value="1" :inactive-value="2" @change="changeTableItem({circle_id:scope.row.circle_id,is_display:scope.row.is_display},formFormatData.api.change.is_show)"  v-model="scope.row.is_display"> </el-switch>
          </template>
         </el-table-column>
        <!-- <el-table-column prop="name" label="状态" >
          <template slot-scope="scope">

            <el-tag type="success" v-if="scope.row.is_display==1">{{'展示中'}}</el-tag>
            <el-tag type="warning" v-if="scope.row.is_display==2">{{'隐藏中'}}</el-tag>
          </template>
        </el-table-column> -->
        <el-table-column label="操作" >
          <template slot-scope="scope">
            <div class="operation-group">
              <el-button class="wlm-text" type="text" @click="showdialog(2,scope.row.circle_id)">编辑</el-button>
              <div class="btn-line"></div>
              <el-button class="wlm-text" type="text" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-content flex-row flex-justify-e flex-align-c">
        <!-- <el-row class="pagination-btns">
          <el-checkbox class="check-all"  true-label="1" false-label="0" v-model="tableFormatData.Circle.files.checkall" @change="toggleSelection">全部</el-checkbox>
          <el-button class="right-8" :disabled="isGroup" size="mini" @click="delTableList">删除</el-button>
        </el-row> -->
        <el-pagination
:disabled="!hasTableData"
@size-change="listPageChange"
@current-change="listPageChange"
        :current-page.sync="tableFormatData.Circle.pagination.page"
:page-sizes="tableFormatData.Circle.pagination.pagesizes"
        :page-size.sync="tableFormatData.Circle.pagination.list_rows"
layout="total, sizes, prev, pager, next, jumper"
         :total="hasTableData?tableFormatData.Circle.pagination.total : 0">
        </el-pagination>
      </div>
    </div>
  </div>
</div>
</template>

<script>
import {
  addCircle,
  editCircle,
  getCircleList,
  delCircle,
  getOneCircle,
  setCircle
} from '@/api/merchant'
import mixins from '@/mixins/mixins'
export default {
  mixins: [mixins.getters('Table'), mixins.getters('Form')],
  name: 'Circle',
  components: {
  },
  created() {
  },
  data() {
    return {
      dialogTableVisible: false,
      selecUseData: [],
      formFormatData: {
        key: 'Circle',
        model: {
          noCreateEdit: false,
          bindDialog: 'dialogTableVisible'
        },
        api: {
          editForm: {// 编辑回填
            api: getOneCircle,
            params: {
              circle_id: ''
            },
            redirect: 'circle_id'
          },
          submitForm: addCircle,
          change: {
            is_show: {
              key: 'is_show',
              api: setCircle
            }
          }
        },
        formData: {
          circle_name: '',
          is_display: 1,
          sort: '',
          default: ''
        },
        rules: {

        }
      },
      tableFormatData: {
        current: 'Circle',
        Circle: {
          key: 'Circle',
          api: {
            getList: getCircleList
            // delList: delCircle
          },
          tableData: [],
          files: {
            circle_name: '',
            ids: [],
            checkall: '0',
            Recycle: '1',
            redirect: 'circle_id'
          },
          pagination: {
            page: 1,
            list_rows: 10,
            pagesizes: [10, 20, 50],
            total: 0
          }
        }
      }
    }
  },
  methods: {
    showdialog(type, params) {
      console.log('type', type)
      if (type === 1) {
        this.formFormatData.api.submitForm = addCircle
        this.dialogTableVisible = true
      }
      if (type === 2) {
        this.formFormatData.formData.default = ''
        this.formFormatData.api.submitForm = editCircle
        this.formFormatDialogEditEvt({ id: params, toggle: 'dialogTableVisible' })
      }
    },

    saveDialogForm() {
      this.resetDialogFormData({ circle_name: '', is_display: 1, sort: '' })
    },
    handleDelete(index, row) { // 删除
      console.log(index, row)
      this.$confirm('此操作将删除此行数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // this.$message({
        //   type: 'success',
        //   message: '删除成功!'
        // });
        delCircle({ circle_id: row.circle_id }).then(res => {
          const { code } = res.data
          code == 1 && this.$message.success('操作成功')
          this.$router.go(0)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.after-sale-reason {
}
</style>
